<?php
/**
 *
 * 版权所有：
 * 作    者：
 * 日    期：2017-02-17
 * 版    本：
 * 功能说明：用户控制器。
 *
 **/

namespace Admin\Controller;
use Admin\Controller\BaseController;

class MemberController extends BaseController
{
    public function index()
    {
        
        $p = isset($_GET['p']) ? intval(I('get.p')) : '1';
        $field = isset($_GET['field']) ? I('get.field') : '';
        $keyword = isset($_GET['keyword']) ? htmlentities(I('get.keyword')) : '';
        $order = isset($_GET['order']) ? I('get.order') : 'DESC';
        $where = '';
        
        //查询条件
        $prefix = C('DB_PREFIX');
        if ($order == 'asc') {
            $order = "{$prefix}user.t asc";
        } elseif (($order == 'desc')) {
            $order = "{$prefix}user.t desc";
        } else {
            $order = "{$prefix}user.uid asc";
        }
        if ($keyword <> '') {
            if ($field == 'user') {
                $where = "{$prefix}user.user LIKE '%$keyword%'";
            }
            if ($field == 'phone') {
                $where = "{$prefix}user.phone LIKE '%$keyword%'";
            }
            if ($field == 'qq') {
                $where = "{$prefix}user.qq LIKE '%$keyword%'";
            }
            if ($field == 'email') {
                $where = "{$prefix}user.email LIKE '%$keyword%'";
            }
        }
        
        $field = "{$prefix}user.*,{$prefix}auth_group.id as gid,{$prefix}auth_group.title";

        $user = M('user');
        //分页
        $pagesize = 10;#每页数量
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $count = $user->field($field)
            ->order($order)
            ->join("{$prefix}auth_group_access ON {$prefix}user.uid = {$prefix}auth_group_access.uid")
            ->join("{$prefix}auth_group ON {$prefix}auth_group.id = {$prefix}auth_group_access.group_id")
            ->where($where)
            ->count();

        $list = $user->field("{$prefix}user.*,{$prefix}auth_group.id as gid,{$prefix}auth_group.title")
            ->order($order)
            ->join("{$prefix}auth_group_access ON {$prefix}user.uid = {$prefix}auth_group_access.uid")
            ->join("{$prefix}auth_group ON {$prefix}auth_group.id = {$prefix}auth_group_access.group_id")
            ->where($where)
            ->limit($offset . ',' . $pagesize)
            ->select();
        
        //dump($user->getLastSql());
        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        $this->assign('list', $list);
        $this->assign('page', $page);
        $group = M('auth_group')->field('id,title')->select();
        $this->assign('group', $group);
        $this->display();
    }

    public function del()
    {
        $uids = isset($_REQUEST['uids']) ? $_REQUEST['uids'] : false;
        //uid为1的禁止删除
        if ($uids == 1 or !$uids) {
            $this->ajaxReturn(array('flag'=>1,'message'=>'参数错误'));
        }
        $del = D('Member')->member_del($uids);
        if ($del) {
            $this->ajaxReturn(array('flag'=>0,'message'=>'删除成功'));
        } else {
            $this->ajaxReturn(array('flag'=>1,'message'=>'删除失败'));
        }
    }
    

    public function add()
    {
        $usergroup = D('member')->user_group();
        $this->assign('usergroup', $usergroup);
        $this->display('form');
    }

    public function edit()
    {
        $uid = isset($_GET['uid']) ? intval($_GET['uid']) : false;
        if ($uid) {
            //$member = M('user')->where("uid='$uid'")->find();
            $prefix = C('DB_PREFIX');
            $user = M('user');
            $user = $user->field("{$prefix}user.*,{$prefix}auth_group_access.group_id")->join("{$prefix}auth_group_access ON {$prefix}user.uid = {$prefix}auth_group_access.uid")->where("{$prefix}user.uid=$uid")->find();
        } else {
            $this->error('参数错误！');
        }

        $usergroup = D('member')->user_group();
        $this->assign('usergroup', $usergroup);

        $this->assign('member', $user);
        $this->display('form');
    }

    public function update($ajax = '')
    {
        //修改权限组
        if ($ajax == 'yes') {
            $uid = I('get.uid', 0, 'intval');
            $gid = I('get.gid', 0, 'intval');
            M('auth_group_access')->data(array('group_id' => $gid))->where("uid='$uid'")->save();
            die('1');
        }
        
        //
        $info = I('post.');
        $uid = isset($info['uid']) ? $info['uid'] : false;
        $user = isset($info['user']) ? $info['user'] : '';
        $group_id = isset($info['group_id']) ? $info['group_id'] : 0;
        if (!$group_id) {
            $this->ajaxReturn(array('flag'=>1,'message'=>'请选择用户组！'));
        }
        $password = isset($info['password']) ? $info['password'] : false;
        if ($password) {
            $data['password'] = password($password);
        }
        $head = I('post.head', '', 'strip_tags');
        $data['sex'] = isset($info['sex']) ? intval($info['sex']) : 0;
        $data['head'] = $head ? $head : '';
        $data['birthday'] = isset($info['birthday']) ? strtotime($info['birthday']) : 0;
        $data['phone'] = isset($info['phone']) ? trim($info['phone']) : '';
        $data['qq'] = isset($info['qq']) ? trim($info['qq']) : '';
        $data['email'] = isset($info['email']) ? trim($info['email']) : '';

        $data['t'] = time();
        
        if (!$uid) {
            if ($user == '') {
                $this->ajaxReturn(array('flag'=>1,'message'=>'用户名称不能为空！'));
            }
            if (!$password) {
                $this->ajaxReturn(array('flag'=>1,'message'=>'用户密码不能为空！'));
            }
            if (M('user')->where("user='$user'")->count()) {
                $this->ajaxReturn(array('flag'=>1,'message'=>'用户名已被占用！'));
            }
            $data['user'] = $user;
            $uid = M('user')->data($data)->add();
            M('auth_group_access')->data(array('group_id' => $group_id, 'uid' => $uid))->add();
            addlog('新增会员，会员UID：' . $uid);
            $this->ajaxReturn(array('flag'=>0,'message'=>'添加会员成功'));
        } else {
            M('auth_group_access')->data(array('group_id' => $group_id))->where("uid=$uid")->save();
            addlog('编辑会员信息，会员UID：' . $uid);
            M('user')->data($data)->where("uid=$uid")->save();
            $this->ajaxReturn(array('flag'=>0,'message'=>'编辑会员信息成功'));
        }
    }

}
